package com.it.schoolbookshop_back.controller.admin;

import com.it.schoolbookshop_back.entities.po.Result;
import com.it.schoolbookshop_back.entities.po.User;
import com.it.schoolbookshop_back.entities.vo.user.UserResetPwd;
import com.it.schoolbookshop_back.entities.vo.user.UserStatistics;
import com.it.schoolbookshop_back.entities.vo.user.UserUpdateRole;
import com.it.schoolbookshop_back.entities.vo.user.UserUpdateStatus;
import com.it.schoolbookshop_back.enums.UserRoleEnums;
import com.it.schoolbookshop_back.enums.UserStatusEnums;
import com.it.schoolbookshop_back.service.AdminService;
import com.it.schoolbookshop_back.utils.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/api/admin")
public class AdminUserManageController {
    @Autowired
    private AdminService adminService;

    /**
     * 管理员获取用户信息
     * @param page 页码，默认1
     * @param limit 每页数量，默认10
     * @param query 搜索关键词（查询用户名、真实姓名、学号）
     * @param status 筛选状态('active', 'suspended', 'deleted')
     * @param role 筛选角色('student', 'admin')
     * @param sortBy 排序字段('created_at', 'username', 'login_at')
     * @return
     */
    @GetMapping("/users")
    public Result<PageBean<User>> getUserInform(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit,
                                String query,String status,String role,String sortBy){

        PageBean<User> pageBean = adminService.getUserInform(page,limit,query,status,role,sortBy);

        return Result.success("获取用户列表成功",pageBean);
    }

    /**
     * 管理员获取用户详情
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public Result<User> getUserDetail(@PathVariable int id){
        User user = adminService.getUserDetail(id);
        return Result.success("获取用户详情成功",user);
    }

    /**
     * 修改用户状态 (使用数字代码)
     * @param id 用户ID
     * @param statusCode 状态代码 (0:ACTIVE, 1:SUSPENDED, 2:DELETED)
     * @return
     */
    @PutMapping("/users/{id}/status/{statusCode}")
    public Result<UserUpdateStatus> updateUserStatusByCode(@PathVariable int id, @PathVariable int statusCode){
        UserStatusEnums statusEnum = UserStatusEnums.getByCode(statusCode);
        if (statusEnum == null) {
            return Result.failed("无效的状态代码");
        }
        
        UserUpdateStatus statusResult = adminService.updateUserStatus(id, statusEnum.name());
        return Result.success("用户状态更新成功", statusResult);
    }
    
    /**
     * 修改用户状态 (使用字符串)
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/users/{id}/status/string/{status}")
    public Result<UserUpdateStatus> updateUserStatus(@PathVariable int id, @PathVariable String status){
        UserUpdateStatus statusResult = adminService.updateUserStatus(id, status);
        return Result.success("用户状态更新成功", statusResult);
    }

    /**
     * 重置用户密码
     * @param id
     * @param resetPassword
     * @return
     */
    @PostMapping("/users/{id}/{resetPassword}")
    public Result<UserResetPwd> resetUserPassword(@PathVariable int id, @PathVariable String resetPassword){
        UserResetPwd pwdResult = adminService.resetUserPassword(id, resetPassword);
        return Result.success("密码重置成功", pwdResult);
    }

    /**
     * 修改用户角色 (使用数字代码)
     * @param id 用户ID
     * @param roleCode 角色代码 (0:STUDENT, 1:ADMIN)
     * @return
     */
    @PutMapping("/users/{id}/role/{roleCode}")
    public Result<UserUpdateRole> updateUserRoleByCode(@PathVariable int id, @PathVariable int roleCode){
        UserRoleEnums roleEnum = UserRoleEnums.getByCode(roleCode);
        if (roleEnum == null) {
            return Result.failed("无效的角色代码");
        }
        
        UserUpdateRole roleResult = adminService.updateUserRole(id, roleEnum.name());
        return Result.success("用户角色更新成功", roleResult);
    }

    /**
     * 修改用户角色 (使用字符串)
     * @param id
     * @param role
     * @return
     */
    @PutMapping("/users/{id}/role/string/{role}")
    public Result<UserUpdateRole> updateUserRole(@PathVariable int id, @PathVariable String role){
        if (role == null) {
            return Result.failed("缺少角色role");
        }
        UserUpdateRole roleResult = adminService.updateUserRole(id, role);
        return Result.success("用户角色更新成功", roleResult);
    }

    /**
     * 获取用户统计数据
     * @return
     */
    @GetMapping("/statistics/users")
    public Result<UserStatistics> getUserStatistics(){
        UserStatistics statistics = adminService.getUserStatistics();
        return Result.success("用户统计数据成功", statistics);
    }

}
